<?php
use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Password;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Regex;
use Phalcon\Validation\Validator\StringLength;

class ServerUsersForm extends Form
{

    /**
     * Initialize the products form
     */
    public function initialize($entity = null, $options = array())
    {
        if (!isset($options['edit'])) {
            $element = new Text("id");
            $this->add($element->setLabel("Id"));
        } else {
            $this->add(new Hidden("id"));
        }

        $user = new Text("user");
        $user->setLabel("平台用户名");
        $user->setFilters(array('striptags', 'string'));
        $user->addValidators(array(
            new PresenceOf(array(
                'message' => '平台用户名是必填项'
            )),
            /*new (array(
                'message' => '平台用户名为英文字母'
            ))*/
        ));
        $this->add($user);

        $name = new Text("name");
        $name->setLabel("用户姓名");
        //$name->setFilters(array('striptags', 'string'));
        $name->addValidators(array(
            new PresenceOf(array(
                'message' => '用户姓名是必填项'
            ))
        ));
        $this->add($name);

        $password = new password("password");
        $password->setLabel("密码");
        $password->setFilters(array('string'));
        $password->addValidators(array(
            new PwdValidator(array(
                'message' => '密码是必填项'
            ))
        ), true);
        $this->add($password);

        $phone = new Text("phone");
        $phone->setLabel("电话");
        $phone->setFilters(array('striptags', 'string'));
        $phone->addValidators(array(
            new PresenceOf(array(
                'message' => '电话是必填项'
            )),
            /*new Regex(array(
                'message' => '电话格式不正确',
                'pattern' => "0\d{2,3}-\d{5,9}|0\d{2,3}-\d{5,9}"  //评注：匹配形式如 0511-4405222 或 021-87888822
            )),
            new StringLength(array(
                'min'     => 7,
                'messageMinimum' => '电话长度不正确'
             ))*/
        ));
        $this->add($phone);

        $mobile = new Text("mobile");
        $mobile->setLabel("手机");
        $mobile->setFilters(array('striptags', 'string'));
        $mobile->addValidators(array(
            new PresenceOf(array(
                'message' => '手机是必填项'
            ))
            /*new StringLength(array( todo 判断长度后保存数据不正确，待修改
                'max'     => 20,
                'messageMinimum' => '手机长度不正确',
            ))*/
        ));
        $this->add($mobile);

        $email = new Text("email");
        $email->setLabel("E-mail");
        $email->setFilters(array('email'));
        $email->addValidators(array(
            new PresenceOf(array(
                'message' => 'E-mail是必填项'
            )),
            new Email(array(
                'message' => 'E-mail是无效的'
            ))
        ));
        $this->add($email);
    }
}